{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 9.4 \u5b9a\u4e49\u4e00\u4e2a\u5e26\u53c2\u6570\u7684\u88c5\u9970\u5668\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5b9a\u4e49\u4e00\u4e2a\u53ef\u4ee5\u63a5\u53d7\u53c2\u6570\u7684\u88c5\u9970\u5668"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6211\u4eec\u7528\u4e00\u4e2a\u4f8b\u5b50\u8be6\u7ec6\u9610\u8ff0\u4e0b\u63a5\u53d7\u53c2\u6570\u7684\u5904\u7406\u8fc7\u7a0b\u3002\n\u5047\u8bbe\u4f60\u60f3\u5199\u4e00\u4e2a\u88c5\u9970\u5668\uff0c\u7ed9\u51fd\u6570\u6dfb\u52a0\u65e5\u5fd7\u529f\u80fd\uff0c\u540c\u65f6\u5141\u8bb8\u7528\u6237\u6307\u5b9a\u65e5\u5fd7\u7684\u7ea7\u522b\u548c\u5176\u4ed6\u7684\u9009\u9879\u3002\n\u4e0b\u9762\u662f\u8fd9\u4e2a\u88c5\u9970\u5668\u7684\u5b9a\u4e49\u548c\u4f7f\u7528\u793a\u4f8b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from functools import wraps\nimport logging\n\ndef logged(level, name=None, message=None):\n    \"\"\"\n    Add logging to a function. level is the logging\n    level, name is the logger name, and message is the\n    log message. If name and message aren't specified,\n    they default to the function's module and name.\n    \"\"\"\n    def decorate(func):\n        logname = name if name else func.__module__\n        log = logging.getLogger(logname)\n        logmsg = message if message else func.__name__\n\n        @wraps(func)\n        def wrapper(*args, **kwargs):\n            log.log(level, logmsg)\n            return func(*args, **kwargs)\n        return wrapper\n    return decorate\n\n# Example use\n@logged(logging.DEBUG)\ndef add(x, y):\n    return x + y\n\n@logged(logging.CRITICAL, 'example')\ndef spam():\n    print('Spam!')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521d\u770b\u8d77\u6765\uff0c\u8fd9\u79cd\u5b9e\u73b0\u770b\u4e0a\u53bb\u5f88\u590d\u6742\uff0c\u4f46\u662f\u6838\u5fc3\u601d\u60f3\u5f88\u7b80\u5355\u3002\n\u6700\u5916\u5c42\u7684\u51fd\u6570 logged() \u63a5\u53d7\u53c2\u6570\u5e76\u5c06\u5b83\u4eec\u4f5c\u7528\u5728\u5185\u90e8\u7684\u88c5\u9970\u5668\u51fd\u6570\u4e0a\u9762\u3002\n\u5185\u5c42\u7684\u51fd\u6570 decorate() \u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u7136\u540e\u5728\u51fd\u6570\u4e0a\u9762\u653e\u7f6e\u4e00\u4e2a\u5305\u88c5\u5668\u3002\n\u8fd9\u91cc\u7684\u5173\u952e\u70b9\u662f\u5305\u88c5\u5668\u662f\u53ef\u4ee5\u4f7f\u7528\u4f20\u9012\u7ed9 logged() \u7684\u53c2\u6570\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b9a\u4e49\u4e00\u4e2a\u63a5\u53d7\u53c2\u6570\u7684\u5305\u88c5\u5668\u770b\u4e0a\u53bb\u6bd4\u8f83\u590d\u6742\u4e3b\u8981\u662f\u56e0\u4e3a\u5e95\u5c42\u7684\u8c03\u7528\u5e8f\u5217\u3002\u7279\u522b\u7684\uff0c\u5982\u679c\u4f60\u6709\u4e0b\u9762\u8fd9\u4e2a\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "@decorator(x, y, z)\ndef func(a, b):\n    pass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u88c5\u9970\u5668\u5904\u7406\u8fc7\u7a0b\u8ddf\u4e0b\u9762\u7684\u8c03\u7528\u662f\u7b49\u6548\u7684;"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def func(a, b):\n    pass\nfunc = decorator(x, y, z)(func)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "decorator(x, y, z) \u7684\u8fd4\u56de\u7ed3\u679c\u5fc5\u987b\u662f\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u5e76\u5305\u88c5\u5b83\uff0c\n\u53ef\u4ee5\u53c2\u80039.7\u5c0f\u8282\u4e2d\u53e6\u5916\u4e00\u4e2a\u53ef\u63a5\u53d7\u53c2\u6570\u7684\u5305\u88c5\u5668\u4f8b\u5b50\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}